/*
 * Credits goes to http://mobiforge.com/developing/story/sms-messaging-android
 */
package emt.friendtracking.smsmodule;

import emt.friendtracking.MyApplication;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class SendSMS
{
	private static final String TAG = SendSMS.class.getSimpleName();
	
	private BroadcastReceiver sent, delivered = null;
	boolean sentRegistered = false, deliveredRegistered =  false;
	
	
	public SendSMS()
	{
	}
	
	class BroadcastSent extends BroadcastReceiver
	{
		@Override
	    public void onReceive(Context arg0, Intent arg1) 
	    {
	        switch (getResultCode())
	        {
	            case Activity.RESULT_OK:
	                Toast.makeText(MyApplication.getAppContext(), "Coordinates sent", 
	                        Toast.LENGTH_SHORT).show();
	                MyApplication.getAppContext().unregisterReceiver(sent);
	                break;
	            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
	                Toast.makeText(MyApplication.getAppContext(), "Generic failure", 
	                        Toast.LENGTH_SHORT).show();
	                MyApplication.getAppContext().unregisterReceiver(sent);
	                break;
	            case SmsManager.RESULT_ERROR_NO_SERVICE:
	                Toast.makeText(MyApplication.getAppContext(), "No service", 
	                        Toast.LENGTH_SHORT).show();
	                MyApplication.getAppContext().unregisterReceiver(sent);
	                break;
	            case SmsManager.RESULT_ERROR_NULL_PDU:
	                Toast.makeText(MyApplication.getAppContext(), "Null PDU", 
	                        Toast.LENGTH_SHORT).show();
	                MyApplication.getAppContext().unregisterReceiver(sent);
	                break;
	            case SmsManager.RESULT_ERROR_RADIO_OFF:
	                Toast.makeText(MyApplication.getAppContext(), "Radio off", 
	                        Toast.LENGTH_SHORT).show();
	                MyApplication.getAppContext().unregisterReceiver(sent);
	                break;
	        }
	    }
	}

	class BroadcastDelivered extends BroadcastReceiver
	{
	    @Override
	    public void onReceive(Context arg0, Intent arg1) 
	    {
	        switch (getResultCode())
	        {
	            case Activity.RESULT_OK:
	                Toast.makeText(MyApplication.getAppContext(), "SMS delivered", 
	                        Toast.LENGTH_SHORT).show();
	                MyApplication.getAppContext().unregisterReceiver(delivered);
	                break;
	            case Activity.RESULT_CANCELED:
	                Toast.makeText(MyApplication.getAppContext(), "SMS not delivered", 
	                        Toast.LENGTH_SHORT).show();
	                MyApplication.getAppContext().unregisterReceiver(delivered);
	                break;                        
	        }
	    }
	}

	public synchronized void sendASMS(String phoneNumber, String message)
	{
		PendingIntent pi = PendingIntent.getActivity(MyApplication.getAppContext(), 0,new Intent(MyApplication.getAppContext(), SendSMS.class), 0);                
	        SmsManager sms = SmsManager.getDefault();
	        Log.d(TAG, "Sending SMS."); //TODO "Sending SMS."
	        sms.sendTextMessage(phoneNumber, null, message, pi, null);  
	        Log.d(TAG, "SMS sent."); //TODO "SMS sent." 
	}
	
	public synchronized void onDestroy()
	{
    	Log.d(TAG, "onDestroyed."); //TODO "onDestroyed."
    	if (sentRegistered)
    		MyApplication.getAppContext().unregisterReceiver(sent);
   		if (deliveredRegistered)
   			MyApplication.getAppContext().unregisterReceiver(delivered);
	}
}
